package com.chixing.controller;


import com.chixing.annotation.AutoIdempotent;
import com.chixing.entity.DTO.OrderCreateDTO;
import com.chixing.service.IOrdersService;
import com.chixing.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author smith
 * @since 2025-09-30
 */
@RestController
@CrossOrigin
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    private IOrdersService ordersService;

    @GetMapping("orderId/{orderId}")
    public Result getOrderDetails(@PathVariable("orderId") Long orderId){
        return ordersService.getOrderDetail(orderId);
    }

    // 分页查询登录用户的订单
    @GetMapping
    public Result getByPageAndStatus(@RequestParam(value = "userId",required = false) Integer userId,
                                    @RequestParam(value = "page",required = false) Integer pageNum,
                                     @RequestParam(value = "status",required = false) String orderStatus){
        if (pageNum == null)
            pageNum = 1;
        return ordersService.getByPageAndStatus(userId,orderStatus,pageNum);
    }

    @GetMapping("{orderId}")
    public Result getByOrderId(@PathVariable("orderId") Long orderId){
        return ordersService.getByOrderId(orderId);
    }

    @PostMapping("/create")
//    @AutoIdempotent //验证幂等性
    public Result createOrder(@RequestBody OrderCreateDTO orderCreateDTO) {
        System.out.println("正在调用11111111111111111111111111111111111");
        return ordersService.createOrder(orderCreateDTO);
    }

    /**
     * 提前取消订单接口
     * @param orderId 包含订单id
     * @return 取消结果（含退款金额、违约说明等）
     */
    @PostMapping("/cancelEarly/{orderId}")
    public Result cancelEarly(@PathVariable Long orderId) {
        return ordersService.orderCancelEarly(orderId);
    }


}
